<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>CCRM管理平台</title>
    <link rel="stylesheet" href="${gPath}/styles/dpl-min.css" />
    <link rel="stylesheet" href="${gPath}/styles/bui-min.css" />
    <link rel="stylesheet" href="${gPath}/styles/main.css" />
    <style type="text/css">
        .detail-text{
            font-size : 17px;
        }

        div.bui-simple-list > ul{
            overflow-x : hidden;
            overflow-y : auto;
            height: 200px;
        }

        #userInfoForm input {
            border-width:0 0 0 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <form id="queryForm" class="form-panel">
            <ul class="panel-content">
                <li>
                    <table>
                        <tr>
                            <shiro:hasPermission name="/clmp/user/getUser">
                            <td style="border-right: 2px #c5c5c5 solid; padding-right: 10px; padding-left: 10px;">
                                手机号码：
                                <span id="queryMobileControl" title="支持通配符*"></span>&nbsp;&nbsp;
                                <button type="button" class="button button-primary" id="queryByMobileBtn">查询</button>
                            </td>
                            </shiro:hasPermission>
                            <shiro:hasPermission name="/clmp/shortcut/getUsersByDevice">
                            <td style="border-right: 2px #c5c5c5 solid; padding-right: 10px; padding-left: 10px;">
                                批次号：
                                <input type="text" class="control-text" id="batchNum" />&nbsp;&nbsp;
                                <button type="button" class="button button-primary" id="queryByBatchBtn">查询</button>
                            </td>
                            <td style="border-right: 2px #c5c5c5 solid; padding-right: 10px; padding-left: 10px;">
                                设备ID：
                                <input type="text" class="control-text" id="deviceId" />&nbsp;&nbsp;
                                <button type="button" class="button button-primary" id="queryByDeviceBtn">查询</button>
                            </td>
                            </shiro:hasPermission>
                            <td style="padding-left: 10px;">
                                <button type="reset" class="button button-primary">重置</button>
                            </td>
                        </tr>
                    </table>
                </li>
            </ul>
        </form>
        <!-- 发送短信对话框 -->
        <div id="MsgModelFormDialog" style="display: none">
            <form id="MsgModelForm" class="form-horizontal">
                <div class="control-group">
                    <label class="control-label">手机号码：</label>
                    <div class="controls">
                        <input type="text" class="control-text" name="mobile" readonly="readonly" style="width: 605px;"/>
                    </div>
                </div>
                <div class="control-group" style="height: 400px;">
                    <label class="control-label">内容：</label>
                    <div class="controls">
                        <textarea name="msg" data-rules="{required:true}" data-messages="{required:'内容不能为空'}" style="resize: none; width: 605px; height: 380px;"></textarea>
                    </div>
                </div>
                <div class="row form-actions actions-bar">
                    <div class="span13 offset3 ">
                        <button type="submit" class="button button-primary">发送</button>
                        <button type="reset" class="button">重置</button>
                    </div>
                </div>
            </form>
        </div>
        <!-- 使用情况对话框 -->
        <div id="UsedInfoDialog" style="display: none">
            <div id="remarkRecordGrid"></div>
        </div>
        <div id="tab"></div>
        <div class="row">
            <div class="span10" style="width: 100%;">
                <div style="padding-top:20px;">
                    <!-- 用户详情tab -->
                    <div id="userInfoTab" style="display: none">
                        <div class="row">
                            <div class="span24">
                                <h1>用户信息</h1>
                                <hr/>
                                <form id="userInfoForm">
                                    <div class="row detail-row">
                                        <div class="span8">
                                            <span><label>姓名：</label><input class="detail-text" name="real_name" readonly="readonly"/></span>
                                        </div>
                                        <div class="span8">
                                            <span><label>电话：</label><input class="detail-text" name="mobile" readonly="readonly"/></span>
                                        </div>
                                        <shiro:hasPermission name="/clmp/shortcut/post_sendMsg:post">
                                        <div class="span8">
                                            <span><button type="button" class="button button-primary" id="sendMsgBtn">发送短信</button></span>
                                        </div>
                                        </shiro:hasPermission>
                                    </div>
                                    <div class="row detail-row">
                                        <div class="span8">
                                            <span><label>生日：</label><input class="detail-text" name="birth_day" readonly="readonly"/></span>
                                        </div>
                                        <div class="span8">
                                            <span><label>性别：</label><input class="detail-text" name="sex" readonly="readonly"/></span>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <div class="row">
                            <form id="remarkSearchForm" class="form-panel">
                                <ul class="panel-content">
                                    <li>
                                        <span class="span-first">
                                            <label>设备ID：</label><input type="text" class="control-text" name="userRemarkInfo.device_id" />
                                        </span>
                                        <span class="span-first">
                                            <label>备注：</label><input type="text" class="control-text" name="userRemarkInfo.remark" />
                                        </span>
                                        <span class="span-first">
                                            <div data-rules="{dateRange : true}">
                                                <span>评论时间：</span>
                                                <input name="start_time" data-tip="{text : '起始日期'}" class="input-small calendar calendar-time" type="text" style="width: 150px;"/>
                                                <label>&nbsp;-&nbsp;</label>
                                                <input name="end_time" data-tip="{text : '结束日期'}" class="input-small calendar calendar-time"  type="text" style="width: 150px;"/>
                                            </div>
                                        </span>
                                        <button type="button" id="remarkSearch" class="button button-primary">搜索</button>
                                    </li>
                                </ul>
                            </form>
                            <div class="search-grid-container">
                                <div id="remarkGrid"></div>
                            </div>
                        </div>
                        <!-- 备注信息对话框 -->
                        <div id="RemarkModelFormDialog" style="display: none">
                            <form id="RemarkModelForm" class="form-horizontal">
                                <input type="hidden" name="userRemarkInfo.user_id" />
                                <div class="control-group">
                                    <label class="control-label">设备ID：</label>
                                    <div class="controls">
                                        <input type="text" class="control-text" name="userRemarkInfo.device_id" data-rules="{number:true}"/>
                                    </div>
                                </div>
                                <div class="control-group" style="height: 400px;">
                                    <label class="control-label">备注：</label>
                                    <div class="controls">
                                        <textarea name="userRemarkInfo.remark" style="resize: none; width: 605px; height: 380px;"></textarea>
                                    </div>
                                </div>
                                <div class="row form-actions actions-bar">
                                    <div class="span13 offset3 ">
                                        <button type="submit" class="button button-primary">保存</button>
                                        <button type="reset" class="button">重置</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <!-- 订单信息tab -->
                    <div id="orderTab" style="display: none">
                        <form id="orderSearchForm" class="form-panel">
                            <ul class="panel-content">
                                <li>
                                    <span>
                                        <label>订单号：</label>
                                        <input type="text" class="control-text" name="order.order_no"/>
                                    </span>
                                    <span>
                                        <label>原订单号：</label>
                                        <input type="text" class="control-text" name="order.original_order_no"/>
                                    </span>
                                    <span>
                                        <label>物流单号：</label>
                                        <input type="text" class="control-text" name="order.express_no"/>
                                    </span>
                                    <span>
                                        <label>订单网名：</label>
                                        <input type="text" class="control-text" name="order.user_name"/>
                                    </span>
                                    <span>
                                        <label>商品编号：</label>
                                        <input type="text" class="control-text" name="order.product_no"/>
                                    </span>
                                    <span>
                                        <label>店铺名：</label>
                                        <input type="text" class="control-text" name="order.shop_name"/>
                                    </span>
                                    <span>
                                        <label>电话：</label>
                                        <input type="text" class="control-text" name="order.phone"/>
                                    </span>
                                    <span>
                                        <label>许可证标志：</label>
                                        <input type="text" class="control-text" name="order.license_token_id"/>
                                    </span>
                                    <button type="button" id="orderSearch" class="button button-primary">搜索</button>
                                </li>
                                <li>
                                    <span>
                                        <span class="span3">订单导入时间：</span>
                                        <div class="bui-form-group span15" data-rules="{dateRange : true}">
                                            <input name="start_time" data-tip="{text : '起始日期'}" class="input-small calendar calendar-time" type="text"/>
                                            <label>&nbsp;-&nbsp;</label>
                                            <input name="end_time" data-tip="{text : '结束日期'}" class="input-small calendar calendar-time"  type="text"/>
                                        </div>
                                    </span>
                                </li>
                            </ul>
                        </form>
                        <h2>最近一个月订单：</h2>
                        <div class="search-grid-container">
                            <div id="orderGrid"></div>
                        </div>
                    </div>
                    <!-- 最近操作设备tab -->
                    <div id="deviceTab" style="display: none">
                        <form id="deviceSearchForm" class="form-panel">
                            <ul class="panel-content">
                                <li>
                                    <span>
                                        <span class="span2">使用时间：</span>
                                        <div class="bui-form-group span15" data-rules="{dateRange : true}">
                                            <input name="start_time" data-tip="{text : '起始日期'}" class="input-small calendar calendar-time" type="text"/>
                                            <label>&nbsp;-&nbsp;</label>
                                            <input name="end_time" data-tip="{text : '结束日期'}" class="input-small calendar calendar-time"  type="text"/>
                                            <button type="button" id="deviceSearch" class="button button-primary">搜索</button>
                                        </div>
                                    </span>
                                </li>
                            </ul>
                        </form>
                        <h2>最近一个月操作设备：</h2>
                        <div class="search-grid-container">
                            <div id="deviceGrid"></div>
                        </div>
                        <!-- 返修信息对话框 -->
                        <div id="RepairDeviceModelFormDialog" style="display: none">
                            <form id="RepairDeviceModelForm" class="form-horizontal">
                                <input type="hidden" name="userRemarkInfo.user_id" />
                                <input type="hidden" name="deviceId" />
                                <div class="control-group" style="height: 150px;">
                                    <label class="control-label">备注：</label>
                                    <div class="controls">
                                        <textarea name="userRemarkInfo.remark" style="resize: none; width: 605px; height: 140px;"></textarea>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label"><input type="checkbox" name="sendMsg" id="sendMsg" value="true" checked="checked"/>&nbsp;&nbsp;发短信</label>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">邮寄地址：</label>
                                    <div class="controls">
                                        <input type="text" class="control-text msgInfo" id="mailingAddress" style="width: 605px;"/>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">收货人：</label>
                                    <div class="controls">
                                        <input type="text" class="control-text msgInfo" id="receiver" style="width: 605px;"/>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">收货人电话：</label>
                                    <div class="controls">
                                        <input type="text" class="control-text msgInfo" id="receiverPhone" style="width: 605px;"/>
                                    </div>
                                </div>
                                <div class="control-group" style="height: 150px;">
                                    <label class="control-label">短信内容：</label>
                                    <div class="controls">
                                        <textarea name="msg" id="msg" style="resize: none; width: 605px; height: 140px;"></textarea>
                                    </div>
                                </div>
                                <div class="row form-actions actions-bar">
                                    <div class="span13 offset3">
                                        <button type="submit" class="button button-primary">保存</button>
                                        <button type="reset" class="button">重置</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript" src="${gPath}/scripts/jquery/jquery-1.8.1.min.js"></script>
    <script type="text/javascript" src="${gPath}/scripts/bui.js"></script>
    <script type="text/javascript" src="${gPath}/scripts/common/search.js"></script>
    <script type="text/javascript">
        BUI.use(['common/search', 'bui/form', 'bui/tab', 'bui/overlay', 'bui/grid', 'bui/select'], function (Search, Form, Tab, Overlay, Grid, Select) {
            var user;
            var tab = new Tab.Tab({
                render : '#tab',
                elCls : 'nav-tabs',
                autoRender: true,
                children:[
                    {text:"客户信息", index:0},
                    {text:"订单信息", index:1},
                    {text:"最近操作设备", index:2}
                ]
            });
            tab.on('selectedchange',function(ev) {
                loadItemData(ev.item.get("index"), user);
            });
            tab.setSelected(tab.getItemAt(0));

            var suggest = new Select.Suggest({
                autoRender: true,
                elCls:null,
                content:'<input type="text" class="control-text" id="mobile"/>',
                render:'#queryMobileControl',
                elAttrs:{'class':''},
                elTagName: 'span',
                dataType:'json',
                name:'mobile',
                url:'${gPath}/clmp/shortcut/post_findUserMobile'
            });

            $("#queryByMobileBtn").click(function(){
                var mobile = $("#mobile").val();
                $.getJSON("${gPath}/clmp/user/getUser", {mobile : mobile}, function(data){
                    userInfoForm.clearFields();
                    if (data.status == 1){
                        user = data.user;
                        var itemIndex = tab.getSelected().get("index");
                        loadItemData(itemIndex, user);
                    } else {
                        BUI.Message.Alert(data.msg, 'info');
                    }
                });
            });

            //其他tab跳到该tab查询,tab之间通过父页面传参
            var params = {};
            if (parent.topManager.get("params")){
                params = parent.topManager.get("params");
                parent.topManager.removeAttr("params");
                $("#mobile").val(params.account);
                var remarkSearchForm = $("#remarkSearchForm");
                remarkSearchForm.find("input[name='start_time']").val(params.start_time);
                remarkSearchForm.find("input[name='end_time']").val(params.end_time);
                $("#queryByMobileBtn").click();
            }

            /**
             * 加载选项卡数据
             */
            function loadItemData(itemIndex, targetUser){
                switch (itemIndex){
                    case 0:
                        if (targetUser){
                            for (var name in targetUser) {
                                if (name == "sex"){
                                    switch (targetUser[name]){
                                        case 0: userInfoForm.setFieldValue(name, "女"); break;
                                        case 1: userInfoForm.setFieldValue(name, "男"); break;
                                        default : userInfoForm.setFieldValue(name, "未知"); break;
                                    }
                                } else {
                                    userInfoForm.setFieldValue(name, targetUser[name]);
                                }
                            }
                            remarkStore.load({
                                'userRemarkInfo.user_id' : targetUser.user_id,
                                start_time : params.start_time,
                                end_time : params.end_time
                            });
                        }
                        $("#userInfoTab").show().siblings("div").hide();
                        break;
                    case 1:
                        if (targetUser)
                            orderStore.load({'order.user_id' : targetUser.user_id});
                        $("#orderTab").show().siblings("div").hide();
                        break;
                    case 2:
                        if (targetUser)
                            deviceStore.load({'user_id' : targetUser.user_id});
                        $("#deviceTab").show().siblings("div").hide();
                        break;
                }
            }

            $("#queryByBatchBtn").click(function(){
                remarkRecordDialog.show();
                $("#deviceId").val(null);
                var batchNum = $("#batchNum").val();
                remarkRecordStore.load({"device.batch_num" : batchNum, "device.device_id" : null});
            });

            $("#queryByDeviceBtn").click(function(){
                remarkRecordDialog.show();
                $("#batchNum").val(null);
                var deviceId = $("#deviceId").val();
                remarkRecordStore.load({"device.device_id" : deviceId,"device.batch_num" : null});
            });

            //备注记录对话框
            var remarkRecordDialog = new Overlay.Dialog({
                title : '设备备注记录',
                width : 800,
                height : 600,
                contentId : 'UsedInfoDialog',
                buttons: []
            });

            var remarkRecordStore;
            var remarkRecordColumns = [
                {dataIndex: 'device_id', visible: false},
                {title: '客户账号', dataIndex: 'account', width: '30%'},
                {title: '备注数量', dataIndex: 'remark_num', width: '30%'},
                {title: '最后备注时间', dataIndex: 'last_remark_time', width: '30%'},
                {title: '操作', dataIndex: '', width: '8%', renderer: function (value, obj) {
                    return '<span class="grid-command btn-info" title="查阅">查阅</span>';
                }}
            ];

            remarkRecordStore = Search.createStore("${gPath}/clmp/shortcut/getUsersByDevice", {
                totalProperty: 'total'
            });
            var remarkRecordGridCfg = Search.createGridCfg(remarkRecordColumns, {
                emptyDataTpl : '<div style="padding: 20px" class="centered"><img src="${gPath}/img/nodata.png"><h2>好像还没有数据</h2></div>'
            });
            var remarkRecordSearch = new Search({
                store: remarkRecordStore,
                autoSearch : false,
                gridCfg : remarkRecordGridCfg,
                gridId : "remarkRecordGrid"
            });
            var remarkRecordGrid = remarkRecordSearch.get('grid');
            remarkRecordGrid.on('cellclick', function (ev) {
                var sender = $(ev.domTarget); //点击的Dom
                if (sender.hasClass('btn-info')) {
                    $("#mobile").val(ev.record.account);
                    $("#queryByMobileBtn").triggerHandler("click");
                    remarkRecordDialog.close();
                }
            });

            //一个月前
            var startTime = addDays(new Date(), -30);
            var start = startTime.getFullYear() +"-"+ (startTime.getMonth()+1) +"-"+ startTime.getDate() + " 00:00:00";
            //当前时间
            var endTime = new Date();
            var end = endTime.getFullYear() +"-"+ (endTime.getMonth()+1) +"-"+ endTime.getDate() +" "+ endTime.getHours() +":"+ endTime.getMinutes() +":"+ endTime.getSeconds();
            /********************************用户信息**********************************/
            var userInfoForm = new Form.HForm({
                srcNode: '#userInfoForm',
                autoRender: true
            });

            var REMARK_URI = "${gPath}/clmp/userRemarkInfo";
            var remarkColumns = [
                {dataIndex: 'id', visible: false},
                {dataIndex: 'account_id', visible: false},
                {dataIndex: 'user_id', visible: false},
                {title: '备注时间', dataIndex: 'create_time', width: '10%'},
                {title: '备注人', dataIndex: 'account', width: '10%', renderer: function (value, obj) {
                    return value ? value.name : null;
                }},
                {title: '备注用户', dataIndex: 'user', width: '10%', renderer: function (value, obj) {
                    return value ? value.nick_name : null;
                }},
                {title: '设备ID', dataIndex: 'device_id', width: '8%'},
                {title: '设备批次号', dataIndex: 'device', width: '8%', renderer: function (value, obj) {
                    return value ? value.batch_num : null;
                }},
                {title: '备注信息', dataIndex: 'remark', width: '60%'}/*,
                {title: '操作', dataIndex: '', width: '8%', renderer: function (value, obj) {
                    var editStr = '<span class="grid-command btn-edit" title="编辑">编辑</span>',
                        delStr = '<span class="grid-command btn-del" title="删除">删除</span>';
                    return editStr + delStr;
            }}*/];
            var remarkStore = Search.createStore("${gPath}/clmp/shortcut/userRemarkInfo", {totalProperty: 'total'});
            var remarkGridCfg = Search.createGridCfg(remarkColumns, {
                tbar: {
                    items: [
                        <shiro:hasPermission name="/clmp/userRemarkInfo:post">
                        {text: '<i class="icon-plus"></i>新建', btnCls: 'button button-small', handler: function () {
                            if (!user){
                                alert("还没查询用户!");
                                return;
                            }
                            remarkModelFormObject.clearFields();
                            remarkModelFormObject.setFieldValue("userRemarkInfo.user_id", user.user_id);
                            remarkModelFormObject.set('action', REMARK_URI);
                            remarkModelFormObject.set('method', "POST");
                            remarkModelFormDialog.show();
                        }}
                        </shiro:hasPermission>/*,
                        {text: '<i class="icon-remove"></i>删除', btnCls: 'button button-small', handler: delRemarkFunction}*/
                    ]
                },
                emptyDataTpl : '<div style="padding: 20px" class="centered"><img src="${gPath}/img/nodata.png"><h2>好像还没有数据</h2></div>',
                plugins: [BUI.Grid.Plugins.CheckSelection] // 插件形式引入多选表格
            });

            var remarkSearch = new Search({
                store: remarkStore,
                autoSearch: false,
                gridCfg: remarkGridCfg,
                gridId : "remarkGrid",
                formId : "remarkSearchForm",
                btnId : "remarkSearch"
            });
            /*var remarkGrid = remarkSearch.get('grid');
            remarkGrid.on('cellclick', function (ev) {
                var sender = $(ev.domTarget); //点击的Dom
                if (sender.hasClass('btn-del')) {
                    delRemarkItems([ev.record]);
                }else if (sender.hasClass('btn-edit')) {
                    editRemark(ev.record);
                }
            });

            function delRemarkFunction() {
                var selections = remarkGrid.getSelection();
                delRemarkItems(selections);
            }

            function delRemarkItems(items) {
                var ids = [];
                BUI.each(items, function (item) {
                    ids.push(item.id);
                });

                if (ids.length) {
                    BUI.Message.Confirm('确认要删除选中的记录么？', function () {
                        $.ajax({
                            url: REMARK_URI + "/" + ids.join("-"),
                            dataType: 'json',
                            type: 'DELETE',
                            data: {ids: ids},
                            success: function (data) {
                                if (data.status == 1) {
                                    BUI.Message.Alert('操作成功');
                                    remarkStore.load();
                                } else {
                                    BUI.Message.Alert('删除失败！');
                                }
                            }
                        });
                    }, 'question');
                }
            }

            function editRemark(record) {
                var id = record.id;
                remarkModelFormObject.clearFields();
                remarkModelFormObject.set('action', REMARK_URI + "/" + id);
                remarkModelFormObject.set('method', "POST");
                for (var name in record) {
                    remarkModelFormObject.setFieldValue(REMARK_MODULE+'.' + name, record[name]);
                }
                remarkModelFormDialog.show();
            }*/

            var remarkModelFormDialog = new Overlay.Dialog({
                title: '备注',
                width: 800,
                height: 600,
                mask: false,
                buttons: [],
                contentId: 'RemarkModelFormDialog'
            });

            var remarkModelFormObject = new Form.HForm({
                srcNode: '#RemarkModelForm',
                submitType: 'ajax',
                autoRender: true,
                callback: function (data) {
                    if (data.status == 1) {
                        BUI.Message.Alert('操作成功');
                        remarkModelFormDialog.close();
                        remarkStore.load();
                    } else {
                        BUI.Message.Alert('操作失败:' + data.msg);
                    }
                }
            });
            /********************************用户信息**********************************/
            /********************************订单信息**********************************/
            var orderGirdColumns = [
                {title: '订单号', dataIndex: 'order_no', width: '10%'},
                {title: '原始订单号', dataIndex: 'original_order_no', width: '10%'},
                {title: '物流单号', dataIndex: 'express_no', width: '10%'},
                {title: '订单网名', dataIndex: 'user_name', width: '10%'},
                {title: '商品编号', dataIndex: 'product_no', width: '10%'},
                {title: '购买数量', dataIndex: 'count', width: '10%'},
                {title: '店铺名', dataIndex: 'shop_name', width: '10%'},
                {title: '电话', dataIndex: 'phone', width: '10%'},
                {title: '地址', dataIndex: 'address', width: '10%'},
                {title: '创建时间', dataIndex: 'create_time', width: '10%'}
            ];
            var orderStore = Search.createStore("${gPath}/clmp/shortcut/order", {params : {'start_time' : start, 'end_time' : end}, totalProperty: 'total'});
            var orderGridCfg = Search.createGridCfg(orderGirdColumns, {
                emptyDataTpl : '<div style="padding: 20px" class="centered"><img alt="Crying" src="${gPath}/img/nodata.png"><h2>好像还没有数据</h2></div>'
            });
            var orderSearch = new Search({
                store: orderStore,
                autoSearch : false,
                gridCfg: orderGridCfg,
                gridId : "orderGrid",
                formId : "orderSearchForm",
                btnId : "orderSearch"
            });
            /********************************订单信息**********************************/
            /********************************最近操作设备信息**********************************/
            var secondColumns = [
                {title: 'ID',dataIndex: 'log_id', width: '5%'},
                {title: '操作指令', dataIndex: 'tn', width: '10%',renderer: function (value) {
                    switch (value){
                        case "00": return "握手";
                        case '01': return "设置操作密码";
                        case '02': return "设定状态保持时间";
                        case "03": return "加解锁设定";
                        case "04": return "设置离场锁定设定";
                        case "05": return "设置有效时间";
                        case "06": return "设置有效期提醒天数";
                        case "30": return "获取组别参数";
                        case "31": return "设置本机冲泡类型与温度";
                        case "32": return "设置水量";
                        case "33": return "设置本机冲泡时间";
                        case "52": return "固件传输确认";
                        case "53": return "固件升级传输";
                        case "54": return "固件升级确认";
                        case "55": return "初始化设备";
                        case "A0": return "AA冲泡";
                        case "A1": return "AA冲泡设定水量";
                        case "A2": return "AA冲泡时间参数";
                        case "61": return "声光功能测试";
                        case "63": return "流速";
                        case "64": return "流量计设置";
                        case "65": return "光耦阈值设置";
                        case "66": return "锅炉功率控制";
                        case "67": return "锅炉N参数设定";
                        case "68": return "锅炉T参数设定";
                        case "69": return "加热系数设定";
                        case "6A": return "温度补偿系数设定";
                        case "6B": return "动态温控模型参数";
                        case "80": return "Echo响应";
                        case "81": return "取本机冲泡记录";
                        case "90": return "重置管理密码";
                        case "F0": return "取设备码";
                        case "F1": return "饮品机时钟设定";
                        default : return value;
                    }
                }},
                {title: 'raw', dataIndex: 'raw', width: '10%'},
                {title: '信息', dataIndex: 'msg', width: '10%'},
                {title: '操作结果', dataIndex: 'result', width: '10%',renderer: function (value) {
                    if(!value)
                        return "未返回";
                    switch (value) {
                        case '00': return "指令解密失败";
                        case '01': return "成功";
                        case '02': return "os有效性验证失败";
                        case '03': return "当前指令无法执行";
                        case '04': return "设备已上锁，无法正常操作";
                        case '05': return "指令无法解析";
                        case '06': return "设备有效期已过，需清洗";
                        default : return value;
                    }
                }},
                {title: '操作时间', dataIndex: 'create_time', width: '10%'},
                {title: 'IP', dataIndex: 'ip', width: '7%'},
                {title: '手机型号', dataIndex: 'mobile_phone_model', width: '7%',renderer: function (value) {
                    switch (value){
                        case 'iPhone3,1': return "iPhone 4";
                        case 'iPhone3,3': return "iPhone 4";
                        case 'iPhone4,1': return "iPhone 4s";
                        case 'iPhone5,1': return "iPhone 5";
                        case 'iPhone5,2': return "iPhone 5";
                        case 'iPhone5,3': return "iPhone 5c";
                        case 'iPhone5,4': return "iPhone 5c";
                        case 'iPhone6,1': return "iPhone 5s";
                        case 'iPhone6,2': return "iPhone 5s";
                        case 'iPhone7,1': return "iPhone 6";
                        case 'iPhone7,2': return "iPhone 6 Plus";
                        case 'iPhone8,1': return "iPhone 6s";
                        case 'iPhone8,2': return "iPhone 6s Plus";
                        default: return value;
                    }
                }},
                {title: '操作系统', dataIndex: 'operating_system', width: '7%'},
                {title: '系统版本', dataIndex: 'operating_system_version', width: '7%'},
                {title: 'APP版本', dataIndex: 'ver_name', width: '7%'},
                {title: '备注', dataIndex: 'remark', width: '10%'}
            ];
            var cascade = new Grid.Plugins.Cascade({
                renderer : function(){
                    return '<div class="inner-grid"></div>';//生成简单表格的容器
                }
            });
            var simpleGridConfig = {
                autoRender:true,
                columns:secondColumns
            };
            cascade.on('expand',function(ev){
                $.getJSON("${gPath}/clmp/shortcut/getLogs", {deviceId:ev.record.device_id, userId:user.user_id}, function(data){
                    var row = ev.row;
                    var container = $(row).find('.inner-grid');
                    container.empty();
                    if(data.length > 0){
                        var gridConfig = BUI.cloneObject(simpleGridConfig);
                        gridConfig.render = container;
                        var sgrid = new Grid.SimpleGrid(gridConfig);
                        sgrid.showData(data);
                    }
                });
            });

            var deviceGirdColumns = [
                {title: 'ID', dataIndex: 'device_id', width: '10%'},
                {title: '设备名', dataIndex: 'nick_name', width: '20%'},
                {title: '批次号', dataIndex: 'batch_num', width: '10%'},
                {title: '硬件版本', dataIndex: 'hardware_version', width: '10%'},
                {title: '固件版本', dataIndex: 'firmware_version', width: '10%'},
                {title: '总使用次数(操作指令数)', dataIndex: 'usedTimes', width: '10%'},
                {title: '最后操作时间', dataIndex: 'lastOperationTime', width: '10%'},
                {title: '设备状态', dataIndex: 'status', width: '10%', renderer: function (value, obj) {
                    switch (value){
                        case 1: return "使用中";
                        case 2: return "测试中";
                        case 3: return "仓储";
                        default : return "未知";
                    }
                }},
                {title: '操作', dataIndex: '', width: '8%', renderer: function (value, obj) {
                    return <shiro:hasPermission name="/clmp/shortcut/post_resetDevice:post">(obj.status != 2 ? '<span class="grid-command btn-reset" title="重置">重置</span>' : '') +</shiro:hasPermission>
                           <shiro:hasPermission name="/clmp/shortcut/post_repairDevice:post">(obj.manage_status != 'M00' ? '<span class="grid-command btn-repair" title="返修">返修</span>' : '')</shiro:hasPermission>;
                }}
            ];
            var deviceStore = Search.createStore("${gPath}/clmp/shortcut/getDevicesByUser", {totalProperty: 'total'});
            var deviceGridCfg = Search.createGridCfg(deviceGirdColumns, {
                emptyDataTpl : '<div style="padding: 20px" class="centered"><img alt="好像还没有数据" src="../img/nodata.png"><h2>好像还没有数据</h2></div>',
                plugins: [cascade] // 插件形式引入级联表格
            });
            var deviceSearch = new Search({
                store: deviceStore,
                autoSearch : false,
                gridCfg: deviceGridCfg,
                gridId : "deviceGrid",
                formId : "deviceSearchForm",
                btnId : "deviceSearch"
            });
            var deviceGrid = deviceSearch.get('grid');
            deviceGrid.on('cellclick', function (ev) {
                var sender = $(ev.domTarget); //点击的Dom
                if (sender.hasClass('btn-reset')) {
                    resetDevice(ev.record);
                } else if (sender.hasClass('btn-repair')) {
                    repairDevice(ev.record);
                }
            });

            var repairDeviceModelFormDialog = new Overlay.Dialog({
                title: '返修详情',
                width: 800,
                height: 700,
                mask: false,
                buttons: [],
                contentId: 'RepairDeviceModelFormDialog'
            });
            repairDeviceModelFormDialog.on("show", function(){
                lastMark = {};
                lastEle = null;
                $("#sendMsg").attr("checked", true);
                $(".msgInfo").attr("disabled", false);
                $("#msg").attr("disabled", false);
            });

            var repairDeviceModelFormObject = new Form.HForm({
                srcNode: '#RepairDeviceModelForm',
                submitType: 'ajax',
                autoRender: true,
                callback: function (data) {
                    if (data.status == 1) {
                        BUI.Message.Alert('操作成功');
                        repairDeviceModelFormDialog.close();
                        remarkStore.load();
                        deviceStore.load();
                    } else {
                        BUI.Message.Alert('操作失败:' + data.msg);
                    }
                }
            });
            repairDeviceModelFormObject.on("beforesubmit", function(){
                if($("#sendMsg").attr("checked")){
                    if ($("#msg").val() == ""){
                        BUI.Message.Alert('内容不能为空！');
                        return false;
                    }
                }
                return true;
            });

            //重置设备
            function resetDevice(record){
                BUI.Message.Confirm('确认要重置吗？', function(){
                    $.post("${gPath}/clmp/shortcut/post_resetDevice", {deviceId : record.device_id, userId : user.user_id}, function(data){
                        if (data.status == 1) {
                            deviceStore.load();
                            remarkStore.load();
                            BUI.Message.Alert('操作成功');
                        } else {
                            BUI.Message.Alert('操作失败');
                        }
                    }, "json");
                });
            }

            //设备返修
            function repairDevice(record){
                repairDeviceModelFormObject.clearFields();
                repairDeviceModelFormObject.setFieldValue("userRemarkInfo.user_id", user.user_id);
                repairDeviceModelFormObject.setFieldValue("userRemarkInfo.remark", "客户提出返修，原因如下：");
                repairDeviceModelFormObject.setFieldValue("msg", "亲爱的"+user.nick_name+"，您的返修申请已登记，请将您的饮品机包装扎实后寄往【地址】，收货人【收货人】，电话【电话】，我们在收到您的返修件及时处理，感谢您的配合！【科贝尔】");
                repairDeviceModelFormObject.setFieldValue("deviceId", record.device_id);
                repairDeviceModelFormObject.set('action', "${gPath}/clmp/shortcut/post_repairDevice");
                repairDeviceModelFormObject.set('method', "POST");
                repairDeviceModelFormDialog.show();
            }

            //填入返修信息时改变短信内容
            var isIE = !+"\v1";
            var eventName = isIE ? "propertychange" : "input";
            var marks = {mailingAddress : "【地址】", receiver : "【收货人】", receiverPhone : "【电话】"};
            var lastMark = {};
            var lastEle = null;
            $(".msgInfo").bind(eventName, function(){
                var msg = $("#msg");
                var regExp = new RegExp(lastEle && lastMark[this.id] ? lastMark[this.id] : marks[this.id],"g");
                var replacement = this.value == ""? marks[this.id]:"【"+ this.value+"】";
                var newMsg = msg.val().replace(regExp, replacement);
                lastMark[this.id] = replacement;
                lastEle = this.id;
                msg.val(newMsg);
            });

            $("#sendMsg").bind("click", function(){
                if (this.checked){
                    $(".msgInfo").attr("disabled", false);
                    $("#msg").attr("disabled", false);
                } else {
                    $(".msgInfo").attr("disabled", true);
                    $("#msg").attr("disabled", true);
                }
            });
            /********************************最近操作设备信息**********************************/
            /********************************发送短信**********************************/
            var msgModelFormDialog = new Overlay.Dialog({
                title: '发送短信',
                width: 800,
                height: 600,
                mask: false,
                buttons: [],
                contentId: 'MsgModelFormDialog'
            });

            var msgModelFormObject = new Form.HForm({
                srcNode: '#MsgModelForm',
                submitType: 'ajax',
                autoRender: true,
                callback: function (data) {
                    if (data.status == 1) {
                        BUI.Message.Alert('发送成功');
                    } else {
                        BUI.Message.Alert('发送失败:' + data.msg);
                    }
                }
            });

            $("#sendMsgBtn").click(function(){
                if (!user){
                    alert("还没查询用户!");
                    return;
                }
                msgModelFormObject.clearFields();
                var mobile = $("#mobile").val();
                if (mobile != ''){
                    msgModelFormObject.setFieldValue("mobile", mobile);
                }
                msgModelFormObject.set('action', "${gPath}/clmp/shortcut/post_sendMsg");
                msgModelFormObject.set('method', "POST");
                msgModelFormDialog.show();
            });
            /********************************发送短信**********************************/
            //增加天数
            function addDays(date, value) {
                date.setDate(date.getDate() + value);
                return date;
            }
        });
    </script>
</body>
</html>